package com.yang.utils;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import com.alibaba.druid.support.json.JSONUtils;

public class RestUtil {

	/**
	 * 运算代码出错 响应500
	 * 
	 * @return
	 */
	public static ResponseEntity<Void> error() {
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}

	/**
	 * 运算代码出错 响应500
	 * 
	 * @return
	 */
	public static <T> ResponseEntity<T> error(T data) {
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(data);
	}

	/**
	 * 正常响应 200
	 * 
	 * @param data
	 * @return
	 */
	public static <T> ResponseEntity<T> success(T data) {
		return ResponseEntity.status(HttpStatus.OK).body(data);
	}

	/**
	 * 正常响应 200
	 * 
	 * @param data
	 * @return
	 */
	public static ResponseEntity<Void> success() {
		return ResponseEntity.status(HttpStatus.OK).body(null);
	}

	/**
	 * 没有找到资源 404
	 * 
	 * @param data
	 * @return
	 */
	public static ResponseEntity<Void> notFound() {
		return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
	}

	/**
	 * 没有找到资源 404
	 * 
	 * @param data
	 * @return
	 */
	public static <T> ResponseEntity<T> notFound(T data) {
		return ResponseEntity.status(HttpStatus.NOT_FOUND).body(data);
	}

	/**
	 * 参数不正确
	 * 
	 * @param data
	 * @return 400
	 */
	public static ResponseEntity<Void> paramError() {
		return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
	}

	/**
	 * 参数不正确
	 * 
	 * @param data
	 * @return 400
	 */
	public static <T> ResponseEntity<T> paramError(T data) {
		return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(data);
	}

	/**
	 * 新资源已经被创建
	 * 
	 * @param POST
	 * @return 201
	 */
	public static ResponseEntity<Void> crtated() {
		return ResponseEntity.status(HttpStatus.CREATED).body(null);
	}

	/**
	 * 新资源已经被创建
	 * 
	 * @param POST
	 * @return 201
	 */
	public static <T> ResponseEntity<T> crtated(T data) {
		return ResponseEntity.status(HttpStatus.CREATED).body(data);
	}

	/**
	 * 请求已经接受
	 * 
	 * @param POST
	 *            PUT DELETE PATCH
	 * @return 202
	 */
	public static ResponseEntity<Void> accepted() {
		return ResponseEntity.status(HttpStatus.ACCEPTED).body(null);
	}

	/**
	 * 请求已经接受
	 * 
	 * @param POST
	 *            PUT DELETE PATCH
	 * @return 202
	 */
	public static <T> ResponseEntity<T> accepted(T data) {
		return ResponseEntity.status(HttpStatus.ACCEPTED).body(data);
	}

	/**
	 * 服务器成功处理了请求
	 * 
	 * @param DELETE
	 *            PUT PATCH
	 * @return 204
	 */
	public static ResponseEntity<Void> noContent() {
		return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
	}

	/**
	 * 服务器成功处理了请求
	 * 
	 * @param DELETE
	 *            PUT PATCH
	 * @return 204
	 */
	public static <T> ResponseEntity<T> noContent(T data) {
		return ResponseEntity.status(HttpStatus.NO_CONTENT).body(data);
	}

	/**
	 * 没权限。服务器收到请求，但拒绝提供服务
	 * 
	 * @return 403
	 */
	public static ResponseEntity<Void> forbidden() {
		return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
	}

	/**
	 * 没权限。服务器收到请求，但拒绝提供服务
	 * 
	 * @return 403
	 */
	public static <T> ResponseEntity<T> forbidden(T data) {
		return ResponseEntity.status(HttpStatus.FORBIDDEN).body(data);
	}

	/**
	 * 请求方式不对
	 * 
	 * @param
	 * @return 405
	 */
	public static ResponseEntity<Void> methodNotAllowed() {
		return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).build();
	}

	/**
	 * 请求方式不对
	 * 
	 * @param
	 * @return 405
	 */
	public static <T> ResponseEntity<T> methodNotAllowed(T data) {
		return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(data);
	}

	/**
	 * 转化为jsonp 返回格式
	 */
	public static <T> String toJsonp(String callback, T data) {
		return callback + "(" + JSONUtils.toJSONString(data) + ")";
	}

}
